
import { RootThunkAction } from "@/types/store"
import { http } from "@/utils/http"
import { ArticleInfoResponse, ArticleCommentResponse, AddArticleResposnse } from "@/types/data"


export const getArticleInfo = (articleId: string): RootThunkAction => {
  return async (dispatch) => {

    const res = (await http.get(`/articles/${articleId}`)) as ArticleInfoResponse 
    dispatch({ type: "article/get", payload: res.data })
  }
}

export const getCommments = (type: string, source: string, offset: string | null, actionType: "append" | "replace"): RootThunkAction => {
  return async (dispatch) => {
    const res = (await http.get("/comments", {
      params: {
        type,
        source,
        offset,
      }, 
    })) as ArticleCommentResponse // 类型断言

    dispatch({ type: "article/getArticleComments", payload: { ...res.data, actionType } })
  }
}

export const addArticleComment = (target: string, content: string): RootThunkAction => {
  return async (dispatch) => {

    const res = (await http.post("/comments", {
      target,
      content,
    })) as AddArticleResposnse
 
    dispatch({ type: "article/addArticleComment", payload: res.data.new_obj })

  }
}
